package com.pearadmin.pro.common.security.subject;

import com.pearadmin.pro.modules.sys.domain.SysUser;
import com.pearadmin.pro.modules.sys.mapper.SysUserMapper;
import com.pearadmin.pro.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Describe: 自定义 Security 用户服务实现
 * Author: 就 眠 仪 式
 * CreateTime: 2019/10/23
 * */
@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    /**
     * Describe: 自定义用户加载策略
     * Param: username
     * */
    @Override
    public CustomUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        CustomUserDetails customUserDetails = sysUserService.lambdaQuery().eq(SysUser::getUserName,username).one();

        if(customUserDetails==null) {

            throw new UsernameNotFoundException("用户名不存在");
        }

        return customUserDetails;
    }
}
